package com.binguo.thirdPay.ruijingPay;

import com.alibaba.fastjson.JSONObject;
import com.binguo.common.exception.ServiceException;
import com.binguo.thirdPay.MD5Util;
import com.binguo.thirdPay.hzShiMing.HZShiMingService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yuritian
 * @Title: ruijingService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020-01-1910:53
 */
@Service
public class RuiJingShopService {

    private static Logger logger = LoggerFactory.getLogger(RuiJingShopService.class);

    private final String URL = "http://pos.ruijinginfo.com";


    public static void main(String[] args) {
        RuiJingShopService ruiJingService = new RuiJingShopService();
        ruiJingService.ruiJingDishTypeSync("YP_20");
    }

    public JSONObject ruiJingDishTypeSync(String deviceid){
        JSONObject param = new JSONObject();
        param.put("deviceid",deviceid);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/dish/type/sync",httpParam);
        return result;
    }

    public JSONObject ruiJingDishSync(String deviceid,
                                       Integer offset,
                                       Integer limit){
        JSONObject param = new JSONObject();
        param.put("deviceid",deviceid);
        param.put("offset",offset);
        param.put("limit",limit);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/dish/sync",httpParam);
        return result;
    }

    public JSONObject ruiJingOrder(String deviceid,
                                   Integer userid,
                                   Integer type,
                                   String code,
                                   List<RuiJingPayForm> payFormList){
        JSONObject param = new JSONObject();
        param.put("deviceid",deviceid);
        param.put("userid",userid);
        param.put("type",type);
        param.put("code",code);
        param.put("list",payFormList);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/order/submit",httpParam);
        return result;
    }

    public JSONObject ruiJingInit(String deviceid){

        JSONObject param = new JSONObject();
        param.put("deviceid",deviceid);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/device/init",httpParam);

        return null;
    }

    public JSONObject ruiJingLogin(String deviceId,
                                   Integer usrId,
                                   String passWord){

        JSONObject param = new JSONObject();
        param.put("deviceid",deviceId);
        param.put("usernum",usrId);
        param.put("pwd",passWord);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/user/login",httpParam);
        return result;
    }

    public JSONObject ruiJingFace(String deviceid,
                                  Integer offset,
                                  Integer limit){

        JSONObject param = new JSONObject();
        param.put("deviceid",deviceid);
        param.put("offset",offset);
        param.put("limit",limit);
        String httpParam = param.toJSONString();
        JSONObject result = getPostRessultJson("/yp/ai/face/sync",httpParam);
        return result;
    }

    private JSONObject getPostRessultJson(String apiUrl,
                                          String httpParam){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        logger.info("瑞景接口"+apiUrl+"发送ruiJingParam:  " + httpParam);
        HttpEntity<String> entity = new HttpEntity<String>(httpParam, headers);
        ResponseEntity<String> resp = restTemplate.postForEntity(URL+apiUrl, entity, String.class);
        String result = resp.getBody();
        logger.info("ruiJingResult:  " + result);
        JSONObject json = JSONObject.parseObject(result);
        if (!apiUrl.equals("/yp/ai/user/login") && !apiUrl.equals("/yp/ai/order/submit")) {
            Integer rst = json.getInteger("rst");
            if (!rst.equals(200)) {
                logger.info("瑞景接口异常");
                return null;
            }
        }
        return json;
    }
}
